{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实例6：用Python给PDF批量加密"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "每个人都有秘密，每个公司也是。我们的很多文件不想给某些人看到，那就可以利用PDF的加密功能设置密码。如果我们生成了很多PDF，一个一个地加密，将费时费力。这种重复的繁重的事儿交给Python，它会不辱使命，你会轻松百倍:)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['data/INV1.pdf',\n",
       " 'data/INV2.pdf',\n",
       " 'data/INV3.pdf',\n",
       " 'data/INV4.pdf',\n",
       " 'data/INV5.pdf']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import PyPDF2 #可从PDF文档提取信息\n",
    "import os #用于获取需要合并的PDF文件所在路径\n",
    "path=\"data/\" # 文件夹路径\n",
    "#1.获取需要加密的文件名及路径\n",
    "files=[]\n",
    "for file in os.listdir(path):\n",
    "    if file.endswith(\".pdf\"): #排除文件夹内的其它干扰文件，只获取PDF文件\n",
    "        files.append(path+file) \n",
    "files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上是先获取我们需要加密的PDF文件所在路径及文件名。详细解释请见实例4。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#2.获取每个PDF文件并加密保存\n",
    "for file in files:\n",
    "    pdf_obj=open(file,'rb')# 以二进制读取，将保留PDF中的所有信息\n",
    "    pdf_reader=PyPDF2.PdfFileReader(pdf_obj)\n",
    "    pdf_writer=PyPDF2.PdfFileWriter()\n",
    "    for page_num in range(pdf_reader.numPages): \n",
    "        page_obj=pdf_reader.getPage(page_num)\n",
    "        pdf_writer.addPage(page_obj)\n",
    "    pdf_writer.encrypt('pass')# 加密,密码设为'pass'，可个性化调整\n",
    "    #写入并保存PDF文件\n",
    "    pdf_output_file=open(file.split(\".\")[0]+\"_sec.pdf\",'wb') #以二进制写入，将保留源PDF中的所有信息\n",
    "    pdf_writer.write(pdf_output_file)\n",
    "    pdf_output_file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们使用`for`循环，逐个给每个PDF文件加密。首先打开PDF文件`pdf_obj=open(file,'rb')`，然后读取信息`pdf_reader=PyPDF2.PdfFileReader(pdf_obj)`，然后建一个写入变量`pdf_writer=PyPDF2.PdfFileWriter()`，随后遍历每个PDF文件的每一页，然后加入到写入变量`pdf_writer`。页面加入完了之后，设置密码`pdf_writer.encrypt('pass')`。密码设置完成后，就保存文件。为避免源文件出问题，此处我们保存为新的文件，命名方式为在源文件名后加上“_sec”。\n",
    "\n",
    "其中`file.split(\".\")[0]+\"_sec.pdf\"`的原理可通过如下理解。更多详细解释请参考实例4。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'data/INV5.pdf'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['data/INV5', 'pdf']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将字符串'data/INV5.pdf'按点号\".\"分成两部分\n",
    "file.split(\".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'data/INV5'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#我们需要的是上面列表中的第一个，因此选[0]\n",
    "file.split(\".\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'data/INV5_sec.pdf'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#连接上新的文件名及后缀\n",
    "file.split(\".\")[0]+\"_sec.pdf\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个处理速度较慢，需要耐心等待。加密完成后的结果如下：\n",
    "![](images\\new_files_with_password.PNG)\n",
    "我们试着打开一个加密后的文件，果然弹出输入密码的对话框，输入我们设定的密码即可进入。\n",
    "![](images\\input_pw.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
